package com.example.oauth2authserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.oauth2authserver.mapper.UserMapper;
import com.example.oauth2authserver.model.User;
import com.example.oauth2authserver.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    private static final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
    @Override
    public User findByUserName(String username) {
        return userMapper.findByUsername(username);
    }

    @Override
    public User create(String username, String password) {
        User user = new User();
        user.setUsername(username);
        user.setPassword("{bcrypt}" + passwordEncoder.encode(password));
        int insert = baseMapper.insert(user);
        return user;
    }
}
